package cn.org.donald.sensitive.factory;

import cn.org.donald.sensitive.model.FactoryEnum;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Stream;

/**
 * @author Donald
 * @since 2023-11-10 13:36:57
 **/
public abstract class IFindSensitive {

    private final Path filePath;

    private final FactoryEnum factoryEnum;

    IFindSensitive(Path filePath, FactoryEnum factoryEnum) {
        this.filePath = filePath;
        this.factoryEnum = factoryEnum;
    }

    public abstract List<String> getSensitiveString(List<String> ruleRegexList);

    public abstract void replace(List<String> ruleRegexList, Path destPath);

    Path getFilePath() {
        return this.filePath;
    }

    FactoryEnum getFactoryEnum() {
        return factoryEnum;
    }

    Stream<String> findSensitiveInString(List<String> ruleRegexList, String string) {
        return ruleRegexList.stream().flatMap(regex -> {
            Pattern pattern = Pattern.compile(regex);
            Matcher matcher = pattern.matcher(string);
            Stream.Builder<String> stream = Stream.builder();
            while (matcher.find()) {
                stream.add(matcher.group());
            }
            return stream.build();
        });
    }
}
